<template>
  <div>
    <el-form :inline="true" :model="page" style="margin-top: 20px;">
      <el-form-item label="博客标题">
        <el-input v-model="page.params.blogTitle" clearable placeholder="博客标题" />
      </el-form-item>
      <el-form-item label="博客分类">
        <el-select v-model="page.params.typeId" filterable clearable placeholder="请选择分类" style="width: 100%">
          <el-option
            v-for="item in typeList"
            :key="item.typeId"
            :label="item.typeName"
            :value="item.typeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-divider />
    <el-button type="primary" size="mini" class="addBtn" @click="openBlogAddDialog()">添加</el-button>
    <el-button type="danger" size="mini" class="addBtn" @click="deleteByIds">批量删除</el-button>
    <el-button type="primary" size="mini" @click="sync('3')">同步</el-button>
    <el-table
      v-loading="loading"
      :data="page.list"
      style="width: 100%"
      element-loading-text="数据加载中"
      element-loading-spinner="el-icon-loading"
      @selection-change="handlerSelectionChange"
      @sort-change="sortChange"
    >
      <el-table-column type="expand"  fixed="left">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.tagNameList.length === 0">暂无标签</el-tag>
          <el-tag v-for="(item,index) in scope.row.tagNameList" :key="index" :color="item.tagColor" style="margin-right: 10px;color: #ffffff">{{item.tagName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="left"
        type="selection"
      />
      <el-table-column
        fixed="left"
        prop="blogTitle"
        label="博客标题"
        width="200"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.blogHot>=1" style="color: red">{{ scope.row.blogTitle }}</span>
          <span v-if="scope.row.blogHot===0">{{ scope.row.blogTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="blogImage"
        label="封面"
        width="150"
        align="center"
      >
        <template slot-scope="scope">

          <el-image
            style="width: 100px;min-height: 60px"
            :src="scope.row.blogImage"
            :preview-src-list="[scope.row.blogImage]"
          >
            <div
              slot="placeholder"
              v-loading="true"
              element-loading-text="图片加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="#fff"
              class="image-slot"
              style="height: 60px;width: 100%"
            />
            <div slot="error" class="image-slot" style="width:100px;height:60px;line-height:60px;background-color: #e2e2e2">
              load failed
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        prop="blogRemark"
        label="博客简介"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="!scope.row.blogRemark" style="color: #20a0ff">{{ '暂无简介' }}</span>
          <span v-else>{{ scope.row.blogRemark }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        prop="status"
        label="发布状态"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status===1" type="success">已发布</el-tag>
          <el-tag v-else type="danger">保存</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        prop="typeName"
        label="分类"
        align="center"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.typeName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        width="90"
        prop="blogGoods"
        label="点赞量"
        align="center"
        sortable="custom"
      />
      <el-table-column
        width="90"
        prop="blogRead"
        label="阅读量"
        align="center"
        sortable="custom"
      >
        <template slot-scope="scope">
          <span style="color: darkred">{{ scope.row.blogRead }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="90"
        prop="blogCollection"
        label="收藏量"
        align="center"
        sortable="custom"
      />
      <el-table-column
        width="90"
        prop="blogComment"
        label="评价量"
        align="center"
        sortable="custom"
      />
      <el-table-column
        width="70"
        prop="blogSource"
        label="来源"
        show-overflow-tooltip
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.blogSource || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column
        width="170"
        prop="updateTime"
        label="更新时间"
        align="center"
        sortable="custom"
      />
      <el-table-column
        width="170"
        fixed="right"
        prop="createdTime"
        label="创建时间"
        align="center"
        sortable="custom"
      />
      <el-table-column
        v-if="isAdmin"
        width="90"
        fixed="right"
        prop="creater"
        label="发布人"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span style="color: #20a0ff"> {{ scope.row.creater || '暂无' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <el-dropdown trigger="hover">
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handlerEdit(scope.row.blogId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handlerPublish(scope.row.blogId)">发布</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handlerBack(scope.row.blogId)">撤回</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-if="isAdmin">
                <el-button size="mini" type="primary" @click="handlerHot(scope.row.blogId)">置顶</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-if="isAdmin">
                <el-button size="mini" type="primary" @click="handlerCancelHot(scope.row.blogId)">取消置顶</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="toRead(scope.row.blogId)">查看</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handlerDelete(scope.row.blogId)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>

      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.countTotal"
      class="pagination-bottom"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!--添加博客-->
    <!-- <el-drawer
      v-if="blogAddDialog"
      title="添加博客"
      :visible.sync="blogAddDialog"
      width="100%"
    >
      <blog-md-add @getBlogList="getBlogList" @closeBlogAddDialog="closeBlogAddDialog" />
    </el-drawer>-->
    <!--<el-dialog
      title="添加博客"
      :visible.sync="blogAddDialog"
      width="84%"
      :close-on-click-modal="false"
    >
      <blog-md-add @getBlogList="getBlogList" @closeBlogAddDialog="closeBlogAddDialog" />
    </el-dialog>-->
    <!--查看帖子-->
    <el-dialog
      :title="blogInfoContent.blogTitle"
      :visible.sync="blogInfoDialog"
      width="60%"
    >
      <div style="overflow-y: auto;height: 600px" v-html="blogInfoContent.blogContent" />
    </el-dialog>
    <!--修改博客弹窗-->
    <!--<el-dialog
      v-if="blogUpdateDialog"
      title="修改博客内容"
      :visible.sync="blogUpdateDialog"
      width="84%"
    >
      <blog-update :blog="blog" @getBlogList="getBlogList" @closeBlogUpdateDialog="closeBlogUpdateDialog" />
    </el-dialog>-->
  </div>
</template>

<script>
import blogApi from '../../../api/blog'
import typeApi from '@/api/type'
import {contentSync} from '@/utils'
export default {
  name: 'BlogList',
  components: {
  },
  data() {
    return {
      page: {
        currentPage: 0, // 当前页
        pageSize: 10, // 页面大小
        pageTotal: 0, // 总页数
        countTotal: 0, // 总条数
        sortColumn: '', // 排序字段
        sortMethod: 'asc', // 排序方法
        list: [], // 数据
        params: {} // 条件查询参数
      },
      blog: {
        blogTitle: '',
        blogImage: null,
        blogContent: '',
        blogType: '',
        blogRemark: null,
        blogSource: ''
      },
      typeList: [],
      blogAddDialog: false,
      blogUpdateDialog: false, // 修改博客内容
      blogInfoDialog: false,
      multipleSelection: [],
      blogInfoContent: {}, // 博客内容
      loading: true,
      isAdmin: false
    }
  },
  watch: {
  },
  created() {
    this.getBlogList()
    this.getTypeList()
    const username = this.$store.getters.username
    if (username === 'admin') {
      this.isAdmin = true
    }
  },
  methods: {
    getTypeList() {
      typeApi.listBack().then(res => {
        this.typeList = res.data
      })
    },
    // 博客同步
    sync(type) {
      this.$confirm('此操作为数据同步, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        tag: 'warning'
      }).then(() => {
        this.loading = true
        // 内容同步
        contentSync(type).then(res=>{
          if(res.code === 200){
            this.$message.success(res.data);
            this.loading = false
            this.getBlogList()
          }else{
            this.$message.error('同步失败')
            this.loading = false
          }
        }).catch(e=>{
          this.loading = false
          this.$message.error('同步失败')
        })
      })
    },
    // 置顶
    handlerHot(blogId) {
      this.hotExecute(1, blogId)
    },
    // 取消置顶
    handlerCancelHot(blogId) {
      this.hotExecute(2, blogId)
    },
    // 置顶
    hotExecute(type, blogId) {
      blogApi.hotExecute(type, blogId).then(res => {
        if (res.code === 200) {
          if (type === 1) {
            this.$message.success('置顶成功')
          }
          if (type === 2) {
            this.$message.success('取消置顶成功')
          }
          this.getBlogList()
        }
      })
    },
    // 发布
    handlerPublish(blogId) {
      blogApi.publish(blogId).then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.getBlogList()
        }
      })
    },
    // 撤回
    handlerBack(blogId) {
      blogApi.back(blogId).then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.getBlogList()
        }
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getBlogList()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getBlogList()
    },
    // 自定义排序
    sortChange(e) {
      this.page.sortColumn = e.prop
      this.page.sortMethod = e.order
      this.getBlogList()
    },
    // 博客修改点击
    handlerEdit(blogId) {
      this.$router.push(`/Article/blogUpdate/${blogId}`)
      /* this.blogUpdateDialog = true
      blogApi.get(blogId).then(res => {
        if (res.code === 200) {
          this.blog = res.data
          this.$message.success('已获取当前博客信息')
        } else {
          this.$message.error('获取当前博客信息失败')
        }
      })*/
    },
    // 多选框事件
    handlerSelectionChange(val) {
      this.multipleSelection = []
      val.forEach(res => {
        this.multipleSelection.push(res.blogId)
      })
    },
    // 多选删除
    deleteByIds(ids) {
      this.$confirm('此操作为批量删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        blogApi.deleteByIds(this.multipleSelection).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.getBlogList()
          } else {
            this.$message.error(res.msg)
            this.loading = false
          }
        }).catch(e => {
          this.loading = false
        })
      })
    },
    getBlogList() {
      this.loading = true
      this.page.params.username = this.$store.getters.username
      blogApi.getByPage(this.page).then(res => {
        this.page = res.data
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    // 后台简单阅读
    toRead(id) {
      this.blogInfoDialog = true
      blogApi.get(id).then(res => {
        if (res.code === 200) {
          this.blogInfoContent = res.data
          this.$message.success('获取博客内容成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 查询
    onSubmit() {
      const loading = this.$loading({
        lock: true,
        text: '查询中请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      blogApi.getByPage(this.page).then(res => {
        if (res.code === 200) {
          this.page = res.data
        } else {
          this.$message.error(res.msg)
        }
        loading.close()
      }, err => {
        this.$message.error(err)
        loading.close()
      })
    },
    handlerDelete(id) {
      this.$confirm('此操作将删除该篇博客, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        blogApi.deleteById(id).then(res => {
          if (res.code === 200) {
            this.getBlogList()
            this.$message.success(res.msg)
          }
        }).catch(e => {
          this.loading = false
        })
      })
    },
    // 打开添加博客弹窗
    openBlogAddDialog() {
      // this.blogAddDialog = true
      this.$router.push('/Article/blogAdd')
    },
    // 关闭添加博客弹窗
    closeBlogAddDialog() {
      this.blogAddDialog = false
    },
    // 打开修改博客弹窗
    openBlogUpdateDialog() {
      this.blogUpdateDialog = true
    },
    // 关闭博客修改弹窗
    closeBlogUpdateDialog() {
      this.blogUpdateDialog = false
    },
    // 打开查看博客弹窗
    openBlogInfoDialog() {
      this.blogAddDialog = true
    },
    // 关闭查看博客弹窗
    closeBlogInfoDialog() {
      this.blogAddDialog = false
    }
  }
}
</script>

<style scoped>
.el-form-item{
  margin-bottom: 0;
}
</style>
